
import json

def send(access_token='', content='', cache_path=''):

    def to_dict(obj) -> dict:
        return obj

    # https://open.dingtalk.com/document/robots/custom-robot-access
    url = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}'
    headers = {
        'Content-Type': 'application/json'
    }

    msg = {
        'msgtype': 'text',
        'text': {
            'content': content
        }
    }
    data = json.dumps(msg)

    import requests
    res = requests.post(url=url, data=data, headers=headers)
    
    if to_dict(json.loads(res.text)).get('errcode', '1') != 0:
        open(cache_path, 'a').write(f"{content}\n")

if __name__ == '__main__':
    send(access_token='', content='', cache_path='')